﻿using Microsoft.AspNetCore.Mvc;
using PmSoft.ApplicationContext;
using PmSoft.Exceptions;
using AppWebApi.Authorization;
using AppWebApi.Models.Dtos;
using Modules.Notify;
using Modules.Entitys;
using AppWebApi.Models;
using Minio.DataModel;

namespace AppCheckInApi.Controller
{
    /// <summary>
    /// 通知接口
    /// </summary>
    [ApiController]
    [Route("/api/v1/app")]
    public class NotifyController : ControllerBase
    {
        private readonly IApplicationContext applicationContext;
        private readonly ILogger logger;
        private readonly SysNotifyService sysNotifyService;
        private readonly WorkerNotifyService workerNotifyService;
        private readonly AutoMapper.IMapper mapper;

        #region 当前请求的用户
        private AppUserInfo _currentUser;
        /// <summary>
        /// 当前请求的用户
        /// </summary>
        private AppUserInfo currentUser
        {
            get
            {
                if (_currentUser != null)
                    return _currentUser;
                if (applicationContext.CurrentUser == null || applicationContext.CurrentUser is not AppUserInfo user)
                    throw new BusinessException("未授权访问");
                _currentUser = user;
                return _currentUser;
            }
        }
        #endregion

        public NotifyController(IApplicationContext applicationContext,ILogger<NotifyController> logger, SysNotifyService sysNotifyService, WorkerNotifyService workerNotifyService, AutoMapper.IMapper mapper)
        {
            this.applicationContext = applicationContext;
            this.logger = logger;
            this.sysNotifyService = sysNotifyService;
            this.workerNotifyService = workerNotifyService;
            this.mapper = mapper;
        }
        /// <summary>
        /// 获取最新的系统消息
        /// </summary>
        /// <returns></returns>
        [HttpGet("sysnotify/new")]
        public ApiResult  NewSysNotify() {

            SysNotifyDto sysNotifyDto = new SysNotifyDto();
            SysNotify top = sysNotifyService.TopNotify();
            if (!string.IsNullOrEmpty(top.Id)) {
                mapper.Map(top, sysNotifyDto);
                return ApiResult.Success("成功", sysNotifyDto).Encrypt();
            }
            SysNotify sysNotify = sysNotifyService.New();
         
            mapper.Map(sysNotify, sysNotifyDto);
            return ApiResult.Success("成功",sysNotifyDto).Encrypt();
        }

        /// <summary>
        /// 获取该员工下所有的工人通知
        /// </summary>
        /// <returns></returns>
        [HttpGet("worknotify/all")]
        public ApiResult GetAllWorkNotify()
        {
            List<WorkerNotify> notifies = workerNotifyService.GetAllWorkerNotifiesByPersonId(currentUser.ObjectId).ToList();
            List<WorkerNotifyListDto> workDtos= new List<WorkerNotifyListDto>();
            foreach (var item in notifies)
            {
                WorkerNotifyListDto workerNotifyDto = new WorkerNotifyListDto();
                mapper.Map(item, workerNotifyDto);
                workDtos.Add(workerNotifyDto);
            }
            return ApiResult.Success("成功", workDtos).Encrypt();
        }

        /// <summary>
        /// 获取工人通知详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("worknotify/{id}")]
        public ApiResult GetWorkNotify(string id)
        {
            WorkerNotify workerNotify= workerNotifyService.GetWorkerNotify(id)??new WorkerNotify();
            workerNotify.ReadedNum++;
            workerNotifyService.UpdateWorkerNotify(workerNotify, new List<string>());
            WorkerNotifyDto notifyDto = new WorkerNotifyDto();
            mapper.Map(workerNotify, notifyDto);
            return ApiResult.Success("成功", notifyDto).Encrypt();
        }


        /// <summary>
        /// 获取系统通知详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("sysnotify/{id}")]
        public ApiResult GetSysnotify(string id)
        {
            SysNotify sysNotify = sysNotifyService.GetSysNotify(id) ?? new SysNotify();
            sysNotify.ReadedNum++;
            sysNotifyService.UpdateSysNotify(sysNotify,new List<string>());
            SysNotifyDto notifyDto = new SysNotifyDto();
            mapper.Map(sysNotify, notifyDto);
            return ApiResult.Success("成功", notifyDto).Encrypt();
        }
    }
}
